home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- *
- * NSSDC/CDF Convert from ROW major to COLUMN major.
- *
- * Version 1.2, 20-Feb-92, ST Systems (STX)
- *
- * Modification history:
- *
- * V1.0 15-May-91, J Love Original version.
- * V1.1 29-Oct-91, J Love Added needed system include files.
- * V1.2 20-Feb-92, J Love Changed to have an input and output buffer
- * rather than switching the majority in place.
- *
- ******************************************************************************/
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
-
- #define MAX_DIMS 10 /* same as CDF_MAX_DIMS */
-
- #if defined(unix)
- #define memmove(a,b,x) bcopy(b,a,x)
- #endif
-
- /******************************************************************************
- * Macro to calculate the indices for a given value offset (row major).
- ******************************************************************************/
-
- #define ROWindices(valueOffset,Ndims,products,indices) { \
- long valueOffsetT = valueOffset; \
- long i; \
- for (i = 0; i < Ndims; i++) { \
- indices[i] = valueOffsetT / products[i]; \
- valueOffsetT = valueOffsetT % products[i]; \
- } \
- }
-
- /******************************************************************************
- * Macro to calculate the value offset for a given set of indices (column
- * major).
- ******************************************************************************/
-
- #define COLoffset(indices,Ndims,products,valueOffset) { \
- long i; \
- valueOffset = 0; \
- for (i = 0; i < Ndims; i++) valueOffset += indices[i] * products[i]; \
- }
-
- /******************************************************************************
- * ROWtoCOL.
- ******************************************************************************/
-
- void ROWtoCOL (iBuffer, oBuffer, Ndims, dimSizes, NvalueBytes)
- void *iBuffer;
- void *oBuffer;
- long Ndims;
- long dimSizes[];
- long NvalueBytes; /* size (in bytes) of each value */
- {
- long indices[MAX_DIMS];
- long ROWproducts[MAX_DIMS];
- long COLproducts[MAX_DIMS];
- long Nvalues;
- long i, j;
-
- /******************************************************************************
- * Calculate the number of values in the array.
- ******************************************************************************/
-
- Nvalues = 1;
- for (i = 0; i < Ndims; i++) Nvalues *= dimSizes[i];
-
- /******************************************************************************
- * Don't do anything except copy if less than 2 dimensions. Row and column
- * major are the same for 0 or 1 dimensions.
- ******************************************************************************/
-
- if (Ndims < 2) {
- memmove (oBuffer, iBuffer, (size_t) (Nvalues * NvalueBytes));
- return;
- }
-
- /******************************************************************************
- * Calculate the row and column products (what each dimension is worth).
- ******************************************************************************/
-
- for (i = 0; i < Ndims; i++) {
- ROWproducts[i] = 1;
- for (j = i + 1; j < Ndims; j++) ROWproducts[i] *= dimSizes[j];
- COLproducts[i] = 1;
- for (j = 0; j < i; j++) COLproducts[i] *= dimSizes[j];
- }
-
- /******************************************************************************
- * For each value in the row major array, calculate its indices and then
- * calculate the corresponding value offset in the column major array (then
- * move it into the column major array).
- ******************************************************************************/
-
- for (i = 0; i < Nvalues; i++) {
- ROWindices (i, Ndims, ROWproducts, indices);
- COLoffset (indices, Ndims, COLproducts, j);
- memmove ((char *) oBuffer + (j * NvalueBytes),
- (char *) iBuffer + (i * NvalueBytes), (size_t) NvalueBytes);
- }
-
- return;
- }
-